package com.megvii.koala.tv.presenter;

import com.megvii.corelib.filechooser.FileChooseEvent;
import com.megvii.corelib.net.Callback;
import com.megvii.corelib.util.AppUtil;
import com.megvii.corelib.util.FileUtil;
import com.megvii.koala.base.KoalaPresenter;
import com.megvii.koala.constance.AppUpdateConstance;
import com.megvii.koala.constance.ErrorCode;
import com.megvii.koala.tv.data.CameraBean;
import com.megvii.koala.tv.data.ServerManager;
import com.megvii.koala.tv.data.SurveillanceCameraManager;
import com.megvii.koala.tv.data.TVBoxManager;
import com.megvii.koala.tv.data.TVGlobalData;
import com.megvii.koala.tv.fragment.BoxSettingFragment;
import com.megvii.koala.tv.fragment.DisplayCameraSettingFragment;
import com.megvii.koala.tv.fragment.RecognizeCamerasSettingFragment;
import com.megvii.koala.tv.fragment.ServerSettingFragment;
import com.megvii.koala.tv.fragment.SettingsFragment;
import com.megvii.koala.tv.fragment.TVBluetoothSettingFragment;
import com.megvii.koala.tv.fragment.param.ServerSettingParam;
import com.megvii.koala.tv.net.TVNetAPI;
import com.megvii.koala.tv.net.tv.GetDisplayConfigResponse;
import com.megvii.koala.tv.net.tv.GetScreenListResponse;
import com.megvii.koala.update.AppUpdateChecker;

import java.util.Collection;

/**
 * Created by linyue on 16/1/22.
 */
public class SettingsPresenter extends KoalaPresenter<SettingsFragment>
        implements SettingsFragment.SettingsListener {
    private TVBoxManager boxManager;

    private ServerManager serverManager;

    private SurveillanceCameraManager cameraManager;

    private AppUpdateChecker updateChecker;

    @Override
    protected void onCreated() {
        super.onCreated();

        boxManager = TVGlobalData.instance().boxManager;
        serverManager = TVGlobalData.instance().serverManager;
        cameraManager = TVGlobalData.instance().cameraManager;

        viewHolder.setListener(this);
        updateChecker = new AppUpdateChecker(viewHolder.getActivity(), viewHolder,
                AppUpdateConstance.FILENAME_PREFIX_TV);
        requestGetScreenList();
        requestGetDisplayConfig();
    }

    @Override
    protected void onResume() {
        super.onResume();
        cameraManager.persistentLoad();
        updateView();
    }

    @Override
    protected void onDestory() {

        updateChecker.release();
    }

    private void updateView() {
        //

        String bluetoothText = "";

        viewHolder.setBluetoothText(bluetoothText);
        //
        String boxAdderss = boxManager.getSelectedAddress();
        String boxText = (boxAdderss != null && !boxAdderss.isEmpty()) ? boxAdderss : "为选择主机";
        viewHolder.setBoxText(boxText);
        //
        String serverAddress = serverManager.getAddress();
        String serverText = (serverAddress != null && !serverAddress
                .isEmpty()) ? serverAddress : "未设置服务器地址";
        viewHolder.setServerText(serverText);
        Collection<CameraBean> recognizeCameras = cameraManager.getRecognizeCameras();
        int cameraAmount = (recognizeCameras != null ? recognizeCameras.size() : 0);
        viewHolder.setRecognizeCameraAmount(cameraAmount);
        CameraBean displayCamera = cameraManager.getPreviewCamera();
        if (displayCamera != null) {
            viewHolder.setDisplayCameraText(
                    displayCamera.camera_position + "(" + displayCamera.camera_address + ")");
        } else {
            viewHolder.setDisplayCameraText("未设置");
        }
        //
        String version = AppUtil.getAppVersion(viewHolder.getActivity());
        viewHolder.setVersionText(version);
        //设置本地媒体文件地址，判断是否存在
        if (!TVGlobalData.instance().fileManager.has_local_file()) {
            TVGlobalData.instance().fileManager.setLocal_theme_path("");
        }
        viewHolder.setDisplayLocalVideoText(FileUtil.getFileName(TVGlobalData.instance().fileManager.getLocal_theme_path()));
    }

    private void requestGetScreenList() {
        String boxToken = boxManager.getSelectedToken();
        viewHolder.showProgressBar();
        TVNetAPI.requestGetScreenList(boxToken, this, this);
    }

    private void requestGetDisplayConfig() {
        String boxToken = AppUtil.getDeviceUUID(viewHolder.getActivity());
        viewHolder.showProgressBar();
        TVNetAPI.requestGetDisplayConfig(boxToken, this, this);
    }

    @Callback(clazz = GetScreenListResponse.class)
    public void onGetScreenListResponse(GetScreenListResponse response) {
        viewHolder.hideProgressBar();
        if (response.code == ErrorCode.NO_ERROR) {
            cameraManager.update(response.data.screens);
            cameraManager.persistentLoad();
            updateView();
        } else {
            showToast(ErrorCode.getDescription(response.code));
        }
    }

    @Callback(clazz = GetDisplayConfigResponse.class)
    public void onGetDisplayConfigResponse(GetDisplayConfigResponse response) {
        viewHolder.hideProgressBar();
        if (response.code == ErrorCode.NO_ERROR) {
            //显示屏幕编号
            String screenId = response.data.device.id + "";
            viewHolder.setDeviceIdText(screenId);
        } else {
            showToast(ErrorCode.getDescription(response.code));
        }
    }

    @Override
    public void onBackPressed() {
        viewHolder.finish();
    }

    @Override
    public void onClickBluetoothSetting() {
        viewHolder.startFragment(TVBluetoothSettingFragment.class, null);
    }

    @Override
    public void onClickBoxSetting() {
        viewHolder.startFragment(BoxSettingFragment.class, null);
    }

    @Override
    public void onClickServerSetting() {
        viewHolder.startFragment(ServerSettingFragment.class, new ServerSettingParam(false));
    }

    @Override
    public void onClickRecognizeCameraSetting() {
        viewHolder.startFragment(RecognizeCamerasSettingFragment.class, null);
    }

    @Override
    public void onClickDisplayCameraSetting() {
        viewHolder.startFragment(DisplayCameraSettingFragment.class, null);
    }

    @Override
    public void onClickLocalFileSetting() {
        viewHolder.showFileChooser();
    }

    @Override
    public void onClickCheckUpdate() {
//        updateChecker.checkUpdate(AppUpdateConstance.TYPE_TV,
//                AppUtil.getAppVersion(viewHolder.getActivity()), true);
        updateChecker.checkUpdate(AppUpdateConstance.TYPE_TV,
                AppUtil.getAppVersion(viewHolder.getActivity()), true);
    }

    public void onEvent(FileChooseEvent event) {
        //刷新界面
        updateView();
    }
}
